package com.seguriapp.administration.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.seguriapp.administration.gwt.client.activity.EditUserDataActivity;
import com.seguriapp.administration.gwt.client.activity.EditUserDataActivity.Display;
import com.seguriapp.entities.User;

public class EditUserDataPage extends Composite implements Display {

  private static EditUserDataPageUiBinder uiBinder = GWT.create(EditUserDataPageUiBinder.class);

  private static final String DEFAULT_ERROR_MSG = "Ha ocurrido un error por favor intente nuevamente la operaci&oacute;n.";
  private static final String MANDATORY_FIELD = "Campo es obligatorio";
  private static final String PASSWORD_REGEX = "^(?=.{8,15}$)(?=.*\\d)(?=.*[a-zA-Z]).*";
  private static final String PASSWORD_ERROR = "Contrase&ntilde;a incorrecta";
  private static final String CONFIRMATION_PASSWORD_ERROR = "Ambas contrase&ntilde;as deben ser iguales";

  interface EditUserDataPageUiBinder extends UiBinder<Widget, EditUserDataPage> {
  }

  private EditUserDataActivity presenter;

  @UiField
  protected HTMLPanel errorMessage;
  @UiField
  protected InlineLabel errorMessageLbl;
  @UiField
  protected HTMLPanel editUserForm;
  @UiField
  protected TextBox username;
  @UiField
  protected PasswordTextBox password;
  @UiField
  protected PasswordTextBox passwordConfirmation;
  @UiField
  protected InlineLabel usernameErrorMensaje;
  @UiField
  protected InlineLabel passwordErrorMensaje;
  @UiField
  protected InlineLabel passwordConfErrorMensaje;

  public EditUserDataPage() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @UiHandler("saveUser")
  protected void onSaveUserClick(final ClickEvent event) {
    if (validateFields()) {
      presenter.saveUser(username.getText(), password.getText());
    } else {
      cleanFields(false);
    }
  }

  private String getSafeHtmlString (final String str) {
    return SafeHtmlUtils.fromTrustedString(str).asString();
  }

  private boolean validateFields() {
    // validate mandatory fields first
    boolean mandatoryFieldsErrors = false;
    if (username.getText() == null || username.getText().isEmpty()) {
      usernameErrorMensaje.setText(MANDATORY_FIELD);
      usernameErrorMensaje.setVisible(true);
      mandatoryFieldsErrors = true;
    }
    if (password.getText() == null || password.getText().isEmpty()) {
      passwordErrorMensaje.setText(MANDATORY_FIELD);
      passwordErrorMensaje.setVisible(true);
      mandatoryFieldsErrors = true;
    }
    if (passwordConfirmation.getText() == null || passwordConfirmation.getText().isEmpty()) {
      passwordConfErrorMensaje.setText(MANDATORY_FIELD);
      passwordConfErrorMensaje.setVisible(true);
      mandatoryFieldsErrors = true;
    }
    if (mandatoryFieldsErrors) {
      return false;
    }

    // validate password and password confirmation
    if (!password.getText().matches(getSafeHtmlString(PASSWORD_REGEX))) {
      passwordErrorMensaje.setText(getSafeHtmlString(PASSWORD_ERROR));
      passwordErrorMensaje.setVisible(true);
      return false;
    }
    if (!password.getText().equals(passwordConfirmation.getText())) {
      passwordConfErrorMensaje.setText(getSafeHtmlString(CONFIRMATION_PASSWORD_ERROR));
      passwordConfErrorMensaje.setVisible(true);
      return false;
    }
    return true;
  }

  private void cleanFields(final boolean includeErrors) {
    username.setText(null);
    password.setText(null);
    passwordConfirmation.setText(null);
    if (includeErrors) {
      cleanErrorMessages();
    }
  }

  private void cleanErrorMessages() {
    usernameErrorMensaje.setText(null);
    passwordErrorMensaje.setText(null);
    passwordConfErrorMensaje.setText(null);
    usernameErrorMensaje.setVisible(false);
    passwordErrorMensaje.setVisible(false);
    passwordConfErrorMensaje.setVisible(false);
  }

  private void setPanelsVisibility(final boolean isError) {
    errorMessage.setVisible(isError);
    editUserForm.setVisible(!isError);
  }

  public void setPresenter(final EditUserDataActivity presenter) {
    this.presenter = presenter;
  }

  public void setEditUserForm(User user) {
    cleanFields(true);
    setPanelsVisibility(false);
    username.setText(user.getUsername());
  }

  public void setNewUserForm() {
    cleanFields(true);
    setPanelsVisibility(false);
  }

  public void setErrorMessage() {
    setPanelsVisibility(true);
  }

  public void setErrorMessage(final String errorMsg) {
    errorMessageLbl.setText(errorMsg);
    setPanelsVisibility(true);
  }

  public void showErrorMessage() {
    errorMessageLbl.setText(getSafeHtmlString(DEFAULT_ERROR_MSG));
    setPanelsVisibility(true);
  }

}
